package com.budwk.app.sqfwV2.service.roomSafety;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.mvc.upload.TempFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: dfxy-shequ
 * @Author: yangweimin
 * @CreateTime: 2024-06-20  16:47
 * @Description: TODO
 */
public class ThirdPartyService {

    private static final String APP_KEY = "1ee8d8a89cdd48b5bbfd0fc0f2364a0a";
    private static String ip = NutzIocUtils.getBean(PropertiesProxy.class).get("room.url");

    /**
     * 获取房屋列表
     * @param regionCityName
     * @param regionCommunityName
     * @param regionVillageName
     * @param houseName
     * @param pageNo
     * @param pageSize
     * @return
     */
    public static Object building(String regionCityName, String regionCommunityName, String regionVillageName, String houseName,
                                      int pageNo, int pageSize) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("regionCityName", regionCityName);
        paramMap.put("regionCommunityName", regionCommunityName);
        paramMap.put("regionVillageName", regionVillageName);
        paramMap.put("houseName", houseName);
        paramMap.put("pageNo", pageNo);
        paramMap.put("pageSize", pageSize);
        HttpResponse httpResponse = HttpRequest.get(ip + "/public/app/building").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 获取字典
     */
    public static Object findDictSelect(String code) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", code);
        HttpResponse httpResponse = HttpRequest.get(ip + "/public/app/findDictSelect").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 获取行政区（通过名称获取）
     */
    public static Object getDistrict(String name) {
        if (StringUtils.isBlank(name)) name = "贵阳市";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        HttpResponse httpResponse = HttpRequest.get(ip + "/public/app/getDistrict").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    public static Object upload(TempFile file) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("file", file.getFile());
        paramMap.put("uploadDir", "building");
        HttpResponse httpResponse = HttpRequest.post(ip + "/public/file/upload").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 获取图片
     * @return
     */
    public static byte[] v_image(String path, String gzip) {
        if (StringUtils.isBlank(path)) return null;
        if (StringUtils.isBlank(gzip)) gzip = "0";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("fileUri", path);
        paramMap.put("fileName", path.substring(path.lastIndexOf("/") + 1));
        paramMap.put("gzip", gzip);
        HttpResponse httpResponse = HttpRequest.get(ip + "/public/file/v_image").form(paramMap).execute();
        return httpResponse.bodyBytes();
    }

    /**
     * 添加房屋
     * @param jsonObject
     * @return
     */
    public static Object addBuilding(JSONObject jsonObject) {
        Map<String, Object> paramMap = BeanUtil.beanToMap(jsonObject);
        HttpResponse httpResponse = HttpRequest.post(ip + "/public/app/addBuilding").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 修改房屋
     * @param jsonObject
     * @return
     */
    public static Object editBuilding(JSONObject jsonObject) {
        Map<String, Object> paramMap = BeanUtil.beanToMap(jsonObject);
        HttpResponse httpResponse = HttpRequest.post(ip + "/public/app/editBuilding").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 获取房屋详情
     * @param id
     * @return
     */
    public static JSONObject queryDetailById(String id) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        HttpResponse httpResponse = HttpRequest.get(ip + "/public/app/queryDetailById").form(paramMap).execute();
        Object o = getResult(httpResponse);
        if (o == null) return null;
        return JSON.parseObject(o.toString());
    }

    /**
     * 获取隐患整改列表（待整改，已整改）
     * @return
     */
    public static Object rectificationList(String regionCityId, String regionCommunityId, String status) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("regionCityId", regionCityId);
        paramMap.put("regionCommunityId", regionCommunityId);
        paramMap.put("status", status);
        HttpResponse httpResponse = HttpRequest.get(ip + "/public/app/rectificationList").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 获取整改详情
     * @return
     */
    public static Object queryRectificationById(String id) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        HttpResponse httpResponse = HttpRequest.get(ip + "/public/app/queryRectificationById").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 房屋整改
     * @param jsonObject
     * @return
     */
    public static Object rectification(JSONObject jsonObject) {
        Map<String, Object> paramMap = BeanUtil.beanToMap(jsonObject);
        HttpResponse httpResponse = HttpRequest.post(ip + "/public/app/rectification").form(paramMap).execute();
        return getResult(httpResponse);
    }

    /**
     * 添加隐患
     * @param jsonObject
     * @return
     */
    public static Object check(JSONObject jsonObject) {
        Map<String, Object> paramMap = BeanUtil.beanToMap(jsonObject);
        HttpResponse httpResponse = HttpRequest.post(ip + "/public/app/check").form(paramMap).execute();
        return getResult(httpResponse);
    }

    private static Object getResult(HttpResponse httpResponse) {
        if (httpResponse.isOk()) {
            ThirdPartyService.Result result = JSONUtil.toBean(httpResponse.body(), ThirdPartyService.Result.class);
            if (result.code == 200) {
                return result.getData();
            }
            throw new ServiceException(result.getMessage());
        }
        throw new ServiceException("请求第三方接口异常");
    }

    /**
     * 第三方调用认证
     * @param request
     * @return
     */
    public static Boolean auth(HttpServletRequest request) {
        String key = request.getHeader("APP_KEY");
        if (!APP_KEY.equals(key)) {
            throw new ServiceException("appKey不正确");
        }
        return false;
    }

    @Data
    public static class Result {
        private Integer code;
        private String message;
        private String error;
        private Object data;
    }
}
